package debtbundler.presentation.preferences;

import java.io.File;
import java.util.Deque;
import java.util.LinkedList;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/**
 * Utility methods for maintaining user preferences specific to Debt Bundler.
 * <p>
 * Application specific user preferences include a list of the three most 
 * recently accessed files.
 * 
 * @author Anssi
 *
 */
public class UserPreferences {
	
	/** Absolute pathname of the most recent file the user has opened. */
	public static final String PREVIOUS_FILE_1 = "prev_file_1";
	/** Absolute pathname of the 2nd most recent file the user has opened. */
	public static final String PREVIOUS_FILE_2 = "prev_file_2";
	/** Absolute pathname of the 3rd most recent file the user has opened. */
	public static final String PREVIOUS_FILE_3 = "prev_file_3";
	/** Prefix of recent file preferences keys. To be used when iterating through recent files */
	protected static final String PREV_FILE_PREFIX = "prev_file_";
	
	/**
	 * Returns a Java <code>Preferences</code> object of the current
	 * user. This is simply a convenience method for calling  
	 * <code>Preferences.userNodeForPackage(UserPreferences.class)</code>.
	 * 
	 * @return	preferences of the current user
	 */
	public static Preferences getUserPreferences() {
		return Preferences.userNodeForPackage(UserPreferences.class);
	}
	
	/**
	 * Sets the most recent file of the current user.
	 * 
	 * @param file	the most recently opened file by the current user
	 * @return		pathnames of the three most recently opened files 
	 * 				after the update
	 * @throws BackingStoreException 
	 * 	if saving the preferences failed
	 * @see			getRecentFiles()
	 */
	public static String[] setMostRecentFile(File file) throws BackingStoreException {
		String mostRecent = file.getAbsolutePath();
		Deque<String> recentFileStack = getRecentFileStack();
		recentFileStack.remove(mostRecent);
		recentFileStack.push(mostRecent);
		
		Preferences prefs = getUserPreferences();
		int i = 1;
		while (!recentFileStack.isEmpty() && i <= 3) {
			prefs.put(PREV_FILE_PREFIX+i, recentFileStack.pop());
			i++;
		}
		prefs.flush();	
		
		return getRecentFiles();
	}

	/**
	 * Returns the pathnames of the three most recent files of the current user 
	 * from the most recent to the 3rd recent.
	 * <p>
	 * The length of the returned array equals the number of recent files. If the
	 * current user has no recent files, an array of zero length is returned.
	 * 
	 * @return	pathnames of the three most recent files of the current user
	 */
	public static String[] getRecentFiles() {		
		return getRecentFileStack().toArray( new String[0] );
	}
	
	/**
	 * @return	pathnames of recent files of current user as a stack
	 * 			with the most recent one on top
	 */
	private static Deque<String> getRecentFileStack() {
		Preferences prefs = getUserPreferences();
		String pathname1 = prefs.get(PREVIOUS_FILE_1, null);
		String pathname2 = prefs.get(PREVIOUS_FILE_2, null);
		String pathname3 = prefs.get(PREVIOUS_FILE_3, null);
		
		Deque<String> pathnames = new LinkedList<String>();
		if (pathname3 != null) pathnames.push(pathname3);
		if (pathname2 != null) pathnames.push(pathname2);
		if (pathname1 != null) pathnames.push(pathname1);
		
		return pathnames; 
	}
}
